package ai;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImgToArray
{
	public static int [][] displayPercent(float [][] imgInfo)
	{
		int [][] result = new int[imgInfo.length][6];
		for(int i = 0; i < imgInfo.length; i++)
		{
			for(int j = 0; j < imgInfo[i].length; j++)
			{
				float imgIf = imgInfo[j][i];
				if((imgIf >= 0) && (imgIf < 0.04))
				{
					result[i][j] = 0;
				}else if((imgIf >= 0.04) && (imgIf < 0.20))
				{
					result[i][j] = 1;
				}else if((imgIf >= 0.20) && (imgIf < 0.40))
				{
					result[i][j] = 2;
				}else if((imgIf >= 0.4) && (imgIf < 0.60))
				{
					result[i][j] = 3;
				}else if((imgIf >= 0.60) && (imgIf < 0.9))
				{
					result[i][j] = 4;
				}
			}
		}
		for(int i = 0; i < result.length; i++)
		{
			for(int j = 0; j < result[i].length; j++)
			{
				System.out.print(result[i][j] + "\t");
			}
			System.out.println("\n");
		}
		return result;
	}
	
	public static void main(String [] args) throws IOException
	{
		BufferedImage bi = ImageIO.read(new File("D:/标准化识别图像.png"));//通过imageio将图像载入  
		int h = bi.getHeight();//获取图像的高  
		int w = bi.getWidth();//获取图像的宽  
		int max = new Color(255, 255, 255).getRGB();
		System.out.println("图像宽高为：" + w + "," + h);
		int perh = h / 6;
		int perw = h / 6;
		float total = perh * perw;
		float [][] imgInfo = new float[6][6];
		for(int i = 0; i < imgInfo.length; i++)
		{
			for(int j = 0; j < imgInfo[i].length; j++)
			{
				int whitePoint = 0;
				for(int k = 0; k < perh; k++)
				{
					for(int l = 0; l < perw; l++)
					{
						int xp = (i * perh) + k;
						int yp = (j * perw) + l;
						if(bi.getRGB(xp, yp) == max)
						{
							whitePoint++;
						}
					}
				}
				float percent = whitePoint / total;
//				System.out.println("白点率为：" + percent);
				imgInfo[i][j] = percent;
			}
		}
		ImgToArray.displayPercent(imgInfo);
	}
}
